package com.example.goodrubish;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.example.goodrubish.Fragment.NewsFragment;
import com.example.goodrubish.Garbage.News_adapter;
import com.example.goodrubish.Garbage.News_list;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class News extends AppCompatActivity {

    private ListView newsList_View;
    public static int News_id;

    private JSONObject news_result= NewsFragment.News_JSON;      //用于接收NewsFragment中的搜索结果
    private List<News_list> newsList=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news);

        ActionBar actionbar=getSupportActionBar();
        if(actionbar!=null){
            actionbar.hide();
        }


        try {
            init_news_list();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        News_adapter adapter=new News_adapter(News.this, R.layout.news_item, newsList);
        ListView listView=findViewById(R.id.news_listview);
        listView.setAdapter(adapter);

        newsList_View=findViewById(R.id.news_listview);
        newsList_View.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                News_id=i;
                String path = "https://www.baidu.com";
                try {
                    path=make_the_String("url");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Uri uri = Uri.parse(path);
                Intent intent=new Intent(Intent.ACTION_VIEW,uri);
                startActivity(intent);
            }
        });
    }

    private void init_news_list() throws JSONException {
        JSONArray newslist=news_result.getJSONArray("newslist");
        for(int i=0;i<newslist.length();i++){
            News_list the_garbage=new News_list(make_the_JSON(i));
            newsList.add(the_garbage);
        }
    }
    private JSONObject make_the_JSON(int i) throws JSONException {            //用于将JSON转换为字符串数组
        JSONArray newslist=news_result.getJSONArray("newslist");
        String[] name=new String[newslist.length()];
        String newslistString=new String();
        JSONObject the_garbage_JSON = new JSONObject();       //具体某一项的json ，可以通过getstring获得其中具体的项
        newslistString=newslist.getString(i);
        the_garbage_JSON=new JSONObject(newslistString);
//        name[i]=the_garbage_JSON.getString("name");
        return the_garbage_JSON;
    }
    private String make_the_String(String need) throws JSONException {            //用于将JSON转换为字符串数组
        JSONArray newslist=news_result.getJSONArray("newslist");
//        String[] name=new String[newslist.length()];
        String newslistString=new String();
        JSONObject the_garbage_JSON=new JSONObject();       //具体某一项的json ，可以通过getstring获得其中具体的项
        newslistString=newslist.getString(News_id);
        the_garbage_JSON=new JSONObject(newslistString);
//        name[i]=the_garbage_JSON.getString("name");
        return the_garbage_JSON.getString(need);
    }
}